
import yaml
import logging
import settings

from logging.config import dictConfig


class Logger(object):
    __instance = None

    def __new__(cls, *args, **kwargs):
        if cls.__instance is None:
            cls.__instance = super().__new__(cls)

        return cls.__instance

    def __init__(self):
        self.logging = logging
        try:
            with open(settings.log_info.get('log_config_path', 'resource/logging-config.yml'), "r") as yaml_file:
                config_dict = yaml.load(yaml_file, Loader=yaml.FullLoader)

                config_dict['handlers']['fileHandler']['level'] = settings.log_info.get('file_log_level', 'INFO')
                config_dict['handlers']['fileHandler']['filename'] = settings.log_info.get('log_path', 'logger.log')
                config_dict['handlers']['consoleHandler']['level'] = settings.log_info.get('console_log_level', 'DEBUG')

            dictConfig(config_dict)

        except Exception as e:
            print(e)

    def get_logger(self, model_name):

        return self.logging.getLogger(model_name)


logger_gen = Logger()

if __name__ == '__main__':
    with open(settings.log_info.get('log_config_path', 'resource/logging-config.yml'), "r") as yaml_file:
        config_dict = yaml.load(yaml_file, Loader=yaml.FullLoader)

        config_dict['handlers']['fileHandler']['level'] = settings.log_info.get('file_log_level', 'INFO')
        config_dict['handlers']['fileHandler']['filename'] = settings.log_info.get('log_path', 'logger.log')
        config_dict['handlers']['consoleHandler']['level'] = settings.log_info.get('console_log_level', 'DEBUG')

    print(config_dict)
    dictConfig(config_dict)